<%@page contentType="text/html;charset=UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
	<title>Web Baesd Intelligent Tutoring System in Sudoku for Logic Training</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <%-- CSS is get from http://www.freelayouts.com/ --%>
    <link rel="stylesheet" type="text/css" href="css/default.css" title="default"/>
</head>

<body>
    <jsp:include page='header.jsp'/>
    <%@ include file='DatabaseSetup.jsp'%>

    <% String loginFormURL =
               "http://144.214.121.25:8080/SudokuITSWebsite/login.jsp";
    String user = (String) session.getAttribute("user");

    if(user == null) {
        response.setHeader("Refresh", "3; URL=" + loginFormURL);
        out.println(
            "<div class=\"article_body\">" +
            "<p><h2>" +
            "You have not login and will be redirected to the login page after 3 seconds. If not, press<a href=\"login.jsp\">here</a><br/>" +
            "您還沒登入！3秒後進入登入頁面.如沒有，請按<a href=\"login.jsp\">此</a><br/>" +
            "</h2></p>" +
            "</div>");
    }else{

    %>

        <div class="article_body">
            <p>
    <%
        int[] Cell = new int[82];
        for(int i=0; i<=81; i++){
            Cell[i] = 0;
        }

        int numGivenCell = 0;

        for(int i=1; i<=81; i++){
            if(request.getParameter(Integer.toString(i))!=null &&
                    !request.getParameter(Integer.toString(i)).equalsIgnoreCase("")){
                Cell[i] = Integer.parseInt(request.getParameter(Integer.toString(i)));
                numGivenCell++;
            }
        }

        String[] CellValue = new String[9];
        for(int i=0; i<9; i++){
            CellValue[i] = "";
        }

        for(int i=1; i<=81; i++){
            if(Cell[i]!=0){
                CellValue[Cell[i]-1] += i + "," ;
            }
        }

        for(int i=0; i<9; i++){
            if(CellValue[i]!=null && !CellValue[i].equalsIgnoreCase("")){
                CellValue[i] = CellValue[i].substring(0, CellValue[i].length()-1);
            }
        }
        String sql = "INSERT INTO websudoku.gamepuzzles(`NumGivenCells`, `DefiniteValue1`, " +
                "`DefiniteValue2`, `DefiniteValue3`, `DefiniteValue4`, `DefiniteValue5`, `DefiniteValue6`, " +
                "`DefiniteValue7`, `DefiniteValue8`, `DefiniteValue9`) VALUES(" +
                "'" + numGivenCell + "', " +
                "'" + CellValue[0] + "', " +
                "'" + CellValue[1] + "', " +
                "'" + CellValue[2] + "', " +
                "'" + CellValue[3] + "', " +
                "'" + CellValue[4] + "', " +
                "'" + CellValue[5] + "', " +
                "'" + CellValue[6] + "', " +
                "'" + CellValue[7] + "', " +
                "'" + CellValue[8] + "')";
        boolean isAdd = (stmt.executeUpdate(sql)>0);

        if(isAdd){
            out.println(sql+"\n");
            out.println("Puzzle Added Successfully!");

        }
    %>
     <br/>
     <a href="ConstructPuzzle.jsp">Construct another puzzle</a>
     </p>
        </div>

    <%

    }
    %>

     <jsp:include page='footer.jsp'/>
</body>
</html>
